﻿using System;
using Microsoft.Xna.Framework;
using System.Drawing;
using System.IO;
using System.Text;

namespace LevelEditor
{
    public static class MatLoader
    {
        public static Bitmap Load(string filename)
        {
            FileInfo file = new FileInfo(filename);
            if (!file.Exists)
                throw new FileNotFoundException();

            int textureWidth = 0, textureHeight = 0;
            System.Drawing.Color[] colorData = null;
            using (BinaryReader reader = new BinaryReader(file.Open(FileMode.Open)))
            {
                textureWidth = reader.ReadInt32();
                textureHeight = reader.ReadInt32();
                colorData = new System.Drawing.Color[textureWidth * textureHeight];
                for (int i = 0; i < colorData.Length; i++)
                    colorData[i] = System.Drawing.Color.FromArgb(reader.ReadByte(), reader.ReadByte(), reader.ReadByte(), reader.ReadByte());
            }

            // create the new bitmap
            Bitmap image = new Bitmap(textureWidth, textureHeight);
            for (int y = 0; y < textureHeight; y++)
            {
                for (int x = 0; x < textureWidth; x++)
                    image.SetPixel(x, y, colorData[x + y * textureWidth]);
            }

            return image;
        }
    }
}